#include <math.h>
#include <iostream>

#ifndef _hhxxttxs_
#define _hhxxttxs_

/*
函数说明：
    int operator=(const Vector& _vec);      向量赋值运算
    bool operator==(const Vector& _vec) const;      判断向量是否相等
    Vector operator-() const;       输出负向量
    Vector operator+(const Vector& _vec) const;     返回两向量之和
    Vector operator-(const Vector& _vec) const;     返回两向量之差
    Vector operator*(const int c) const;        向量数乘运算
    double operator*(const Vector& _vec) const;     向量点乘运算
    friend Vector operator*(const int c,Vector& _vec);      二元运算,数乘
    double L2norm() const;      向量范数
*/

class Vector{
    private:
    double *coord;
    int dim;
    public:
    Vector();
    Vector(int _d);
    Vector(double *_start, double *_end);
    ~Vector(){delete [] coord;};
    void print();
    int get_dim () const;
    //double get_coord (int _idx) const;
    void set_coord (int _idx, double _val);
    double& operator[](int _idx) const;
    int operator=(const Vector& _vec);
    bool operator==(const Vector& _vec) const;
    Vector operator-() const;
    Vector operator+(const Vector& _vec) const;
    Vector operator-(const Vector& _vec) const;
    Vector operator*(const int c) const;
    double operator*(const Vector& _vec) const;
    friend Vector operator*(const int c,Vector& _vec);
    double L2norm() const;
};

Vector::Vector(){
    dim=0;
    coord = new double[dim];
}

Vector::Vector(int _d){
    dim=_d;
    coord=new double[dim];
}

Vector::Vector(double *_start, double *_end){
    double* temp=_start;
    dim=_end-_start;
    coord=new double[dim];
    double* p = coord;
    do{
        *p++=*temp++;
    }while ((temp!=_end));
}

void Vector::print(){
    std::cout<<"(";
    for(int i=0;i<dim;i++)
    std::cout<<coord[i]<<", ";
    std::cout<<"\b\b) "<<std::endl;
}

int Vector::get_dim() const{
    return dim;
}

void Vector::set_coord (int _idx, double _val){
    *(coord+_idx)=_val;
}

double & Vector::operator[](int _idx) const{
    return *(coord+_idx);
}
/*double Vector::get_coord (int _idx) const{
    return *(coord+idx);
}*/

int Vector::operator=(const Vector& _vec){
    dim=_vec.get_dim();
    coord=new double[dim];
    for(int i=0;i<dim;i++){
        coord[i]=_vec[i];
    }
    return 0;
}

bool Vector::operator==(const Vector& _vec) const{
    if (dim != _vec.get_dim())
    {
        return false;
    }
    for (int i = 0; i < dim; i++)
    {
        if (coord[i] != _vec[i])
        {
            return false;
        }
    }
    return true;
    
}

Vector Vector::operator-() const{
    Vector result(dim);
    for (int i = 0; i < dim; i++)
    if(coord[i]!=0)
    result.set_coord( i , -coord[i] );
    return result;
}

Vector Vector::operator+(const Vector& _vec) const{
    Vector result(dim);
    if (dim != _vec.get_dim())
    {
        std::cout << "mismatch" << std::endl;
        return result;
    }
    
    for (int i = 0; i < dim; i++)
    {
        result.set_coord(i,coord[i] + _vec[i]);
    }
    return result;
}

Vector Vector::operator-(const Vector& _vec) const{
    Vector result(dim);
    if (dim != _vec.get_dim())
    {
        std::cout << "mismatch" << std::endl;
        return result;
    }
    
    for (int i = 0; i < dim; i++)
    {
        result.set_coord(i,coord[i] - _vec[i]);
    }
    return result;
}

Vector Vector::operator*(const int c) const{
    Vector result(dim);
    for (int i = 0; i < dim; i++)
    result.set_coord(i,c*coord[i]);
    return result;
}

double Vector::operator*(const Vector& _vec) const{
    double result = 0;
    if (dim != _vec.get_dim())
    {
        std::cout << "mismatch" << std::endl;
        return result;
    }
    for (int i = 0; i < dim; i++)
    {
        result += coord[i]*_vec[i];
    }
    return result;
}

Vector operator*(const int c,Vector& _vec){
    return _vec*c;
}//*/

double Vector::L2norm() const{
    double result = 0;
    for (int i = 0; i < dim; i++)
    result += fabs(coord[i]);
    return result;
}

#endif